package com.ssm.anno.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @author 张富玮
 * @create 2022-01-28 21:06
 */
@Configuration // 声明配置类
@ComponentScan("com.ssm.anno.service") // 组件扫描
@EnableTransactionManagement // 开启事务支持
@EnableAspectJAutoProxy // 开启 AOP 注解支持
public class SpringServiceConfig {

    /**
     * 创建事务管理器
     *
     * @param dataSource
     * @return
     */
    @Bean
    public TransactionManager getTransactionManager(DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();

        transactionManager.setDataSource(dataSource);

        return transactionManager;
    }
}
